功欲善其事,必先利其器。先來安裝好撰寫 purescript 需要的工具罷。
Purescript 本質上會把 source compile 成 javascript 再在 browser 或是 node 上運行,所以我們會先安裝它的 compiler: purs
(以 typescript 來做比喻的話,就是 tsc
)
首先我們需要安裝 node
: https://nodejs.org/en/download/package-manager/
我個人是使用 macOS 和 homebrew 的。打開 terminal:
> brew install node
接下來使用 npm
安裝 purs
。由於個人比較習慣使用 yarn
,所以實際演示會使用 yarn
:
# 如果使用 npm 的話,輸入 `npm install -g purescript`
> yarn global add purescript
yarn global v1.22.15
[1/4] ? Resolving packages...
warning purescript > purescript-installer > request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
warning purescript > purescript-installer > request > har-validator@5.1.5: this library is no longer supported
warning purescript > purescript-installer > request > uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
[2/4] ? Fetching packages...
[3/4] ? Linking dependencies...
[4/4] ? Building fresh packages...
warning Your current version of Yarn is out of date. The latest version is "1.22.19", while you're on "1.22.15".
info To upgrade, run the following command:
$ curl --compressed -o- -L https://yarnpkg.com/install.sh | bash
success Installed "purescript@0.15.4" with binaries:
- purs
✨ Done in 13.50s.
# 檢查安裝是否成功
> purs
Usage: purs COMMAND
The PureScript compiler and tools
Available options:
--version Show the version number
-h,--help Show this help text
Available commands:
bundle This command was removed in v0.15.0. Run this command
for migration information.
compile Compile PureScript source files
docs Generate documentation from PureScript source files
in a variety of formats, including Markdown and HTML
graph Module dependency graph
hierarchy Generate a GraphViz directed graph of PureScript type
classes
ide Start or query an IDE server process
publish Generates documentation packages for upload to
Pursuit
repl Enter the interactive mode (PSCi)
For help using each individual command, run `purs COMMAND --help`. For example,
`purs compile --help` displays options specific to the `compile` command.
purs 0.15.4
如果 purs
運行成功應該就 okay 了,接下來安裝 package manager spago
:
> yarn global add spago
成功以後我們就可以來建立新 project 了!
> mkdir purs-project
> cd purs-project
> spago init
❯ yarn run spago init
[info] Initializing a sample project or migrating an existing one..
[info] Updating package-set tag to "psc-0.15.4-20220901"
Fetching the new one and generating hashes.. (this might take some time)
[info] Generating new hashes for the package set file so it will be cached.. (this might take some time)
[info] Set up a local Spago project.
[info] Try running `spago build`
我們可以看到 spago 建立了幾個檔案,先來看看 src/Main.purs
:
module Main where
import Prelude
import Effect (Effect)
import Effect.Console (log)
main :: Effect Unit
main = do
log "?"
這是個很簡單的 hello world,讓我們來執行一下他:
> spago build
[info] Installing 3 dependencies.
[info] Searching for packages cache metadata..
[info] Unable to find packages cache metadata, downloading from GitHub..
[warn] Unable to download GitHub metadata, global cache will be disabled
[info] Installing "console"
[info] Installing "effect"
[info] Installing "prelude"
[info] Installation complete.
[ 3 of 58] Compiling Record.Unsafe
[ 1 of 58] Compiling Type.Proxy
[ 4 of 58] Compiling Data.Unit
# 省略
[56 of 58] Compiling Main
[57 of 58] Compiling Effect.Class.Console
[58 of 58] Compiling Test.Main
[info] Build succeeded.
✨ Done in 32.33s.
> spago run
[info] Build succeeded.
?
到這裡就成功啦!